package edu.nuist.javaProjct.view;

import edu.nuist.javaProjct.dao.UserBasicDao;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class LoginForm {
    private JButton loginBtn;
    private JTextField accountField;
    private JTextField passwordField;
    private JPanel loginForm;

    static JFrame loginFrame = new JFrame("登陆界面");

    private UserBasicDao userBasicDao=new UserBasicDao();

    public LoginForm() {
        loginBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String name=accountField.getText();
                String password=passwordField.getText();
                boolean isLoginSuccess=userBasicDao.queryUserAuth(name,password);
                if (isLoginSuccess){
                    showMainForm();
                    loginFrame.setVisible(false);
                }else{
                    JOptionPane.showMessageDialog(loginForm, "请检查用户名和密码", "请注意",JOptionPane.WARNING_MESSAGE);
                }
            }
        });
    }

    public static void main(String[] args) {
        loginFrame.setContentPane(new LoginForm().loginForm);

        loginFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        loginFrame.pack();
        loginFrame.setVisible(true);
        loginFrame.setSize(500,500);
    }

    public void showMainForm(){
        JFrame frame = new JFrame("主界面");
        frame.setContentPane(new MainForm().mainForm);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
        frame.setSize(500,500);
    }
}
